package com.yunhe.openapi.util;

import com.yunhe.openapi.model.CompanyInfo;
import com.yunhe.openapi.model.CustomerInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.yaml.snakeyaml.Yaml;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

//客户编号工具类
public class CustomerUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerUtil.class);
    private static List<CustomerInfo> customerList = new ArrayList<>();

    //解析customer.yml文件
    public static void parseCustomerYml() {
        String filePath = "classpath:customer.yml";
        InputStream in = null;
        try {
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            Resource[] resources = resolver.getResources(filePath);
            Resource resource = resources[0];//获得文件流，因为在jar文件中，不能直接通过文件资源路径拿到文件，但是可以在jar包中拿到文件流
            in = resource.getInputStream();
            Yaml props = new Yaml();
            Object obj = props.loadAs(in,Map.class);

            Map<String, Object> map = (Map<String, Object>) ((Map) obj).get("openId");
            parseToCustomerList(map);

        } catch (FileNotFoundException e) {
            LOGGER.error(e.getMessage(),e);
        } catch (IOException e) {
            LOGGER.error(e.getMessage(),e);
        } finally {
            if (in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    LOGGER.error(e.getMessage(),e);
                }
            }
        }
    }

    //数据解析到list
    private static void parseToCustomerList(Map<String,Object> map) {
        map.forEach( (customerId, data) ->{
            //客户信息
            CustomerInfo customerInfo = new CustomerInfo();
            customerInfo.setCustomerId(customerId); //客户id

            Map<String, Object> subMap = (Map<String, Object>) map.get(customerId);
            customerInfo.setCustomerName((String) subMap.get("name")); //客户名称

            //公司信息
            List<CompanyInfo> companys = new ArrayList<>();
            Map<String, Object> companyMap = (Map<String, Object>) subMap.get("companys");
            companyMap.forEach( (code, obj) ->{
                CompanyInfo companyInfo = new CompanyInfo();
                companyInfo.setCode(code);
                companyInfo.setName((String) ((LinkedHashMap) obj).get("name"));
                companyInfo.setPointNumbers((Map<String, String>) ((LinkedHashMap) obj).get("pointNumbers"));
                companys.add(companyInfo);
            });


            customerInfo.setCompanys(companys);

            customerList.add(customerInfo);
        } );
    }

    public static CustomerInfo getByCustomerId(String customerId) {
        for(int i=0; i<customerList.size(); i++){
            if(customerList.get(i).getCustomerId().equals(customerId)){
                return customerList.get(i);
            }
        }
        return null;
    }

    /*public static void main(String[] a) {
        parseCustomerYml();
    }*/
}
